﻿using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RabbitMQ_Service.Services
{
    public class RabbitMQBaseService
    {
        /// <summary>
        /// 获取链接
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <param name="username"></param>
        /// <param name="userpwd"></param>
        /// <param name="virtualhost"></param>
        /// <param name="heartbeat"></param>
        /// <returns></returns>
        public static IConnection GetConnection(string ip, int port, string username, string userpwd, string virtualhost, ushort heartbeat)
        {
            try
            {
                ConnectionFactory factory = new ConnectionFactory();
                factory.Port = port;
                factory.Endpoint = new AmqpTcpEndpoint(new Uri("amqp://" + ip + "/"));
                factory.UserName = username;
                factory.Password = userpwd;
                factory.VirtualHost = virtualhost;
                factory.RequestedHeartbeat = heartbeat;
                return factory.CreateConnection();
            }
            catch (Exception ex) {
                throw ex;
            }
        }
    }
}
